<template>
  <view class="main-layout">
    <!-- 页面内容区域 -->
    <view class="page-content">
      <slot></slot>
    </view>
    
    <!-- 底部导航栏 -->
    <view class="tab-bar">
      <view 
        v-for="(item, index) in tabList" 
        :key="index" 
        class="tab-item" 
        :class="{ active: currentTab === index }" 
        @click="switchTab(index, item.path)"
      >
        <image 
          :src="currentTab === index ? item.activeIcon : item.inactiveIcon" 
          class="tab-icon"
        ></image>
        <text class="tab-text" :class="{ 'active-text': currentTab === index }">{{ item.text }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'TabBar',
  props: {
    // 当前激活的选项卡索引
    active: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentTab: this.active,
      tabList: [
        {
          text: '首页',
          path: '/pages/index/index',
          activeIcon: '/static/Images/menu/Home-s.png',
          inactiveIcon: '/static/Images/menu/Home-a.png'
        },
        {
          text: '商品分类',
          path: '/pages/goods/index',
          activeIcon: '/static/Images/menu/Menu-s.png',
          inactiveIcon: '/static/Images/menu/Menu-a.png'
        },
        {
          text: '消息',
          path: '/pages/notice/index',
          activeIcon: '/static/Images/menu/Text-s.png',
          inactiveIcon: '/static/Images/menu/Text-a.png'
        },
        {
          text: '任务大厅',
          path: '/pages/tasks/index',
          activeIcon: '/static/Images/menu/Order-s.png',
          inactiveIcon: '/static/Images/menu/Order-a.png'
        },
        {
          text: '个人中心',
          path: '/pages/users/index',
          activeIcon: '/static/Images/menu/My-s.png',
          inactiveIcon: '/static/Images/menu/My-a.png'
        }
      ]
    }
  },
  watch: {
    active: {
      handler(newVal) {
        this.currentTab = newVal;
      },
      immediate: true
    }
  },
  methods: {
    switchTab(index, path) {
      if (this.currentTab === index) return;
      this.currentTab = index;
      this.$emit('change', index);
      
      // 跳转到对应页面
      uni.switchTab({
        url: path,
        fail: () => {
          // 如果不是 tabBar 页面，使用 navigateTo
          uni.navigateTo({
            url: path
          });
        }
      });
    }
  }
}
</script>

<style scoped>
.main-layout {
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
}

.page-content {
  flex: 1;
  padding-bottom: 100rpx; /* 为底部导航栏预留空间 */
}

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  z-index: 999;
  padding-bottom: env(safe-area-inset-bottom);
  padding-bottom: constant(safe-area-inset-bottom); /* iOS 11.0 */
  margin: 0 auto;
  width: 750rpx;
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 10rpx 0;
  transition: all 0.2s ease;
  position: relative;
}

.tab-item.active {
  transform: scale(1.05);
}

.tab-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 6rpx;
  transition: all 0.2s ease;
}

.tab-text {
  font-size: 22rpx;
  color: #999999;
  line-height: 1;
  transition: all 0.2s ease;
}

.active-text {
  color: #FF5151;
  font-weight: 500;
}
</style>